{-# LANGUAGE ForeignFunctionInterface #-}
 
module E_Data where
 
import Foreign
import Foreign.Ptr
import Foreign.C.String
import Foreign.C.Types

       -- types.h needed for t_off
#include </usr/include/arm-linux-gnueabihf/sys/types.h>
#include </opt/adapteva/esdk/tools/host/include/epiphany-hal-data.h>

newtype E_Bool = E_Bool {unE_Bool :: CInt}
    deriving (Eq, Show)
#{enum E_Bool, E_Bool
    , e_true  = E_TRUE
    , e_false = E_FALSE
    }

{-newtype E_loader_diag = E_loader_diag {unE_loader_diag :: CInt}
    deriving (Eq, Show)
#{enum E_loader_diag, E_loader_diag
    , e_D0  = L_D0
    , e_D1  = L_D1
    , e_D2  = L_D2
    , e_D3  = L_D3
    , e_D4  = L_D4
    }
-}
data Emem = Emem                        -- we never do anything with Emem in Haskell, so leave it opaque
instance Storable Emem where
    sizeOf    _ = (#size e_mem_t)
    alignment _ = alignment (undefined :: CDouble)
    peek ptr = do
        return Emem
    poke ptr Emem = do
        return ()

type EmemRef = Ptr Emem
 
{-corresponds to -e_epiphany_t -}
data EpiphanyInfo = EpiphanyInfo { e_objtype :: CInt, e_type :: CInt, e_num_cores :: CInt, e_base_coreid :: CInt, e_row :: CInt, e_col :: CInt, e_rows :: CInt, e_cols :: CInt, e_core :: CInt, e_memfd :: CInt}
    deriving (Show)
type GroupRef = Ptr EpiphanyInfo

instance Storable EpiphanyInfo where
    sizeOf    _ = (#size e_epiphany_t)
    alignment _ = alignment (undefined :: CDouble)
    peek ptr = do
        objtype         <- (#peek e_epiphany_t, objtype) ptr    --object type identifier
        tipe            <- (#peek e_epiphany_t, type) ptr       -- Epiphany chip part number
        num_cores       <- (#peek e_epiphany_t, num_cores) ptr  -- number of cores group
        base_coreid     <- (#peek e_epiphany_t, base_coreid) ptr-- group base core ID
        row             <- (#peek e_epiphany_t, row) ptr
        col             <- (#peek e_epiphany_t, col) ptr
        rows            <- (#peek e_epiphany_t, rows) ptr
        cols            <- (#peek e_epiphany_t, cols) ptr
        core            <- (#peek e_epiphany_t, core) ptr
        memfd           <- (#peek e_epiphany_t, memfd) ptr
        return  EpiphanyInfo { e_objtype=objtype, e_type=tipe, e_num_cores=num_cores, e_base_coreid=base_coreid, e_row=row, e_col=col, e_rows=rows, e_cols=cols, e_core=core, e_memfd=memfd }
 
    poke ptr (EpiphanyInfo e_objtype e_type e_num_cores e_base_coreid e_row e_col e_rows e_cols e_core e_memfd) = do
        (#poke e_epiphany_t, objtype) ptr e_objtype
        (#poke e_epiphany_t, type) ptr e_type
        (#poke e_epiphany_t, num_cores) ptr e_num_cores
        (#poke e_epiphany_t, base_coreid) ptr e_base_coreid
        (#poke e_epiphany_t, row) ptr e_row
        (#poke e_epiphany_t, col) ptr e_col
        (#poke e_epiphany_t, rows) ptr e_rows
        (#poke e_epiphany_t, cols) ptr e_cols
        (#poke e_epiphany_t, core) ptr e_core
        (#poke e_epiphany_t, memfd) ptr e_memfd
 
{-corresponds to -e_platform_t -}
data PlatformInfo = PlatformInfo { p_objtype::CInt, p_type::CInt, p_version::CInt, p_hal_ver::CInt, p_initialized::CInt, p_regs_base::CInt, p_num_chips::CInt, p_chip::CInt, p_row::CInt, p_col::CInt, p_rows::CInt, p_cols::CInt, p_num_emems::CInt, p_emem::CInt}
    deriving (Show)
 
instance Storable PlatformInfo where
    sizeOf    _ = (#size e_platform_t)
    alignment _ = alignment (undefined :: CDouble)
    peek ptr = do
        objtype         <- (#peek e_platform_t, objtype) ptr    -- object type identifier
        tipe            <- (#peek e_platform_t, type) ptr       -- platform part number
        version         <- (#peek e_platform_t, version) ptr    -- version number of platform
        hal_ver         <- (#peek e_platform_t, hal_ver) ptr    -- version number of the E-HAL
        initialized     <- (#peek e_platform_t, initialized) ptr-- platform initialized?
        regs_base       <- (#peek e_platform_t, regs_base) ptr  -- base address of platform registers
        num_chips       <- (#peek e_platform_t, num_chips) ptr  -- number of Epiphany chips in platform
        chip            <- (#peek e_platform_t, chip) ptr       -- pointer to array of Epiphany chip objects
        row             <- (#peek e_platform_t, row) ptr
        col             <- (#peek e_platform_t, col) ptr
        rows            <- (#peek e_platform_t, rows) ptr
        cols            <- (#peek e_platform_t, cols) ptr
        num_emems       <- (#peek e_platform_t, num_emems) ptr  --
        emem            <- (#peek e_platform_t, emem) ptr       --
        return  PlatformInfo {
            p_objtype=objtype,
            p_type=tipe,
            p_version=version,
            p_hal_ver=hal_ver,
            p_initialized=initialized,
            p_regs_base=regs_base,
            p_num_chips=num_chips,
            p_chip=chip,
            p_row=row,
            p_col=col,
            p_rows=rows,
            p_cols=cols,
            p_num_emems=num_emems,
            p_emem=emem }
    poke ptr (PlatformInfo p_objtype p_type p_version p_hal_ver p_initialized p_regs_base p_num_chips p_chip p_row p_col p_rows p_cols p_num_emems p_emem) = do
        (#poke e_platform_t, objtype) ptr p_objtype        -- object type identifier
        (#poke e_platform_t, type) ptr p_type              -- platform part number
        (#poke e_platform_t, version) ptr p_version        -- version number of platform
        (#poke e_platform_t, hal_ver) ptr p_hal_ver        -- version number of the E-HAL
        (#poke e_platform_t, initialized) ptr p_initialized-- platform initialized?
        (#poke e_platform_t, regs_base) ptr p_regs_base    -- base address of platform registers
        (#poke e_platform_t, num_chips) ptr p_num_chips    -- number of Epiphany chips in platform
        (#poke e_platform_t, chip) ptr p_chip              -- array of Epiphany chip objects
        (#poke e_platform_t, row) ptr p_row                -- platform absolute minimum row number
        (#poke e_platform_t, col) ptr p_col                -- platform absolute minimum col number
        (#poke e_platform_t, rows) ptr p_rows              -- number of rows in platform
        (#poke e_platform_t, cols) ptr p_cols              -- number of cols in platform
        (#poke e_platform_t, num_emems) ptr p_num_emems    -- number of external memory segments in platform
        (#poke e_platform_t, emem) ptr p_emem              -- array of external memory segments
 
 

